<template>
  <el-container>
    <el-main>
      <el-form el-form ref="form" :model="searchList" label-width="80px">
        <el-row>
          <el-col :span="4">
            <el-form-item label="部门名称">
              <el-input
                v-model="searchList.uname"
                @input="searchAllData"
                placeholder="根据名称模糊搜索"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-table
        ref="multipleTable"
        :data="searchList.uname ? searchedData : tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55"> </el-table-column> -->

        <el-table-column prop="name" label="部门名称" width="120">
        </el-table-column>
        <el-table-column prop="remake" label="详细信息" show-overflow-tooltip>
        </el-table-column>

        <el-table-column label="操作" show-overflow-tooltip v-if="role()">
          <template slot-scope="scope" v-has="'ROLE_admin'">
            <el-button
              size="mini"
              v-has="'ROLE_admin'"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              v-has="'ROLE_admin'"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 弹出的修改框 -->
      <el-dialog title="修改部门" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="部门名称" :label-width="formLabelWidth">
            <el-input
              v-model="form.name"
              autocomplete="off"
              style="width: 220px"
            ></el-input>
          </el-form-item>
          <el-form-item label="详细描述" :label-width="formLabelWidth">
            <el-input
              v-model="form.remake"
              autocomplete="off"
              style="width: 220px"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleEditCancel">取 消</el-button>
          <el-button type="primary" @click="handleEditCommit">确 定</el-button>
        </div>
      </el-dialog>
    </el-main>

    <!-- 页脚分页 -->
    <div class="fenye">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-container>
</template>

<script>
export default {
  name: "BuMen",
  data() {
    return {
      allData: [],
      tableData: [],
      searchedData: [],
      page: 1, //页脚显示的当前页
      size: 10, //一页多少条数据
      total: 400, //总条目数

      multipleSelection: [], //当前选中的行
      searchList: {
        //搜索条件的
        uname: "",
        uclass: "",
      },
      dialogFormVisible: false, //弹出的修改表单
      form: {
        id: "",
        name: "",
        remake: "",
      },
      formLabelWidth: "120px", //宽度
    };
  },
  methods: {
    searchAllData() {
      // console.log(this.searchList.uname);
      // console.log(this.searchedData);
      // console.log(this.allData[0].name);
      if (this.searchList.uname != "") {
        this.searchedData = [];
        var reg = new RegExp(this.searchList.uname);
        for (let i = 0; i < this.allData.length; i++) {
          if (this.allData[i].name.match(reg)) {
            console.log(this.allData[i]);
            this.searchedData.push(this.allData[i]);
          }
        }
      }
    },
    //分页表格数据
    getTableData() {
      //allData为全部数据
      this.tableData = this.allData.slice(
        (this.page - 1) * this.size,
        this.page * this.size
      );
      this.total = this.allData.length; //总条目等于allDaTa长度
    },
    //size改变时的回调，参数为当前的size
    handleSizeChange(val) {
      // console.log("改变每页多少条，当前一页多少条", val);
      this.size = val;
      this.page = 1; //size改变时要把page重置为1，再把参数赋值给size，因为size改变会导致page数量改变
      this.getTableData();
    },
    //page改变时的回调函数，参数为当前页码
    handleCurrentChange(val) {
      // console.log("翻页，当前为第几页", val);
      this.page = val;
      this.getTableData();
    },
    //请求所有的部门信息
    getAllDept() {
      this.$request.get("dept/queryAll").then((res) => {
        // console.log(res);
        if (res.data.obj != null) {
          // console.log(res.data.obj);
          this.allData = res.data.obj;
          // console.log(this.allData);
          this.getTableData(); //获取信息后更新表单数据
        } else {
          this.$message.error("信息获取失败");
        }
      });
    },
    role() {
      // console.log("111");
      const role = window.sessionStorage.getItem("authority");
      // console.log(role);
      if (role == "ROLE_admin") {
        return true;
      }
      return false;
    },
    reset() {
      this.getAllDept();
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.dialogFormVisible = true;
      this.form = row;
    },
    handleEditCommit() {
      this.$request
        .post("dept/updateById", {
          id: this.form.id,
          name: this.form.name,
          remake: this.form.remake,
        })
        .then((res) => {
          if (res.data.obj != null) {
            this.$message.success("修改成功");
            this.form.id = "";
            this.form.name = "";
            this.form.remake = "";
            this.reset();
          }
        });

      this.dialogFormVisible = false;
    },
    handleEditCancel() {
      this.dialogFormVisible = false;
    },

    handleDelete(index, row) {
      console.log(row.id);
      this.$request.get("dept/deleteById/" + row.id).then((res) => {
        if (res.data.obj == true) {
          this.$message("删除成功");
          this.reset();
        } else {
          this.$message.error("删除失败失败");
        }
      });
    },
  },
  mounted() {
    this.getTableData();
    this.getAllDept();
  },
};
</script>

<style scoped>
.el-header {
  background-color: rgb(42, 53, 66);
  color: #fff;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
.el-col {
  /* background-color: #fff; */
  color: #fff;
}
i {
  font-size: 18px;
}
.fenye {
  position: fixed;
  bottom: 10px;
}
</style>